package com.wanpinghui.supplier;

import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.os.Bundle;

import com.umeng.analytics.MobclickAgent;
import com.wanpinghui.supplier.message.nim.NimManager;
import com.wanpinghui.supplier.wxapi.AppKeyConstant;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.yunshl.yunshllibrary.manager.MActivityManager;
import com.yunshl.yunshllibrary.storage.LogManager;
import com.yunshl.yunshllibrary.storage.ShareDataManager;
import com.yunshl.yunshllibrary.storage.StorageManager;

import org.xutils.x;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/11/21
 ***/

public class MApplication extends Application {
    private Activity topActivity;
    public static MApplication context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        ShareDataManager.getInstance().init(this, "wphApp");
        x.Ext.init(this);
        WPHSdk.init(this);
       WPHSdk.initTencent(AppKeyConstant.QQ_APP_ID);
        WPHSdk.initWeChat(AppKeyConstant.WX_APP_ID);
        StorageManager.initFilePath(this, "wphApp");
        LogManager.init(this);
        NimManager.getInstance().init(this);
        MobclickAgent.setCatchUncaughtExceptions(true);
        activityLifeObserver();
    }

    public static MApplication getAContext() {
        return context;
    }

    /**
     * Activity生命周期记录
     */
    public void activityLifeObserver() {
        if (Build.VERSION.SDK_INT >= 14) {
            registerActivityLifecycleCallbacks(
                    new ActivityLifecycleCallbacks() {
                        @Override
                        public void onActivityCreated(
                                Activity activity, Bundle bundle) {
                            MActivityManager.getInstance().addACT(activity);
                        }

                        @Override
                        public void onActivityStarted(Activity activity) {
                        }

                        @Override
                        public void onActivityResumed(Activity activity) {
                            topActivity = activity;
                        }

                        @Override
                        public void onActivityPaused(Activity activity) {
                        }

                        @Override
                        public void onActivityStopped(Activity activity) {
                        }

                        @Override
                        public void onActivitySaveInstanceState(
                                Activity activity, Bundle bundle) {
                        }

                        @Override
                        public void onActivityDestroyed(Activity activity) {
                            MActivityManager.getInstance().remove(activity);
                        }
                    });

        }
    }


    public Activity getTopActivity() {
        return topActivity;
    }

}
